using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using Microsoft.ApplicationBlocks.Data;
using Microsoft.AceEngineering.SPM.SPMCustomDataEntity;

namespace Microsoft.AceEngineering.SPM.SPMDataLayer
{
    public class ScoreRangeTaskDL
    {
        public static List<ScoreRangeTaskCDE> GetScoreRangeTask(int id, string sortExpr, string dir, bool active)
        {
            SqlDataReader sqlreader = null;
            List<ScoreRangeTaskCDE> scoreList = new List<ScoreRangeTaskCDE>();
            ScoreRangeTaskCDE score = null;
            try
            {
                string conString = ConfigurationManager.ConnectionStrings["SPMSQL"].ConnectionString;

                SqlParameter[] myParams = new SqlParameter[4];
                myParams[0] = new SqlParameter("ScoreRangeID", SqlDbType.Int);
                myParams[0].Value = id;

                myParams[1] = new SqlParameter("@active", SqlDbType.Bit);
                myParams[1].Value = active;

                myParams[2] = new SqlParameter("@dir", SqlDbType.VarChar);
                myParams[2].Value = dir;

                myParams[3] = new SqlParameter("@sortExpr", SqlDbType.VarChar);
                myParams[3].Value = sortExpr;

                sqlreader = SqlHelper.ExecuteReader(conString, CommandType.StoredProcedure, "SPM_spGetScoreTaskMapping", myParams);

                while (sqlreader.Read())
                {
                    score = new ScoreRangeTaskCDE();
                    score.LifeCycleName = sqlreader["LifeCycleName"].ToString();
                    score.TaskID = sqlreader["TaskID"].ToString();
                    score.TaskName = sqlreader["TaskName"].ToString();
                    scoreList.Add(score);
                    score = null;
                }
            }
            finally
            {
                if (sqlreader != null)
                {
                    if (!sqlreader.IsClosed)
                        sqlreader.Close();
                }
            }
            return scoreList;
        }

        public static void DeleteMapping(int scoreID, string taskID)
        {
            string conString = ConfigurationManager.ConnectionStrings["SPMSQL"].ConnectionString;

            SqlParameter[] myParams = new SqlParameter[2];
            myParams[0] = new SqlParameter("@scoreRangeID", SqlDbType.Int);
            myParams[0].Value = scoreID;

            myParams[1] = new SqlParameter("@taskID", SqlDbType.VarChar);
            myParams[1].Value = taskID;

            SqlHelper.ExecuteNonQuery(conString, CommandType.StoredProcedure, "SPM_spUpdateScoreTaskMapping", myParams);
        }

        public static void CreateMapping(int scoreID, string taskID, string createdBy)
        {
            string conString = ConfigurationManager.ConnectionStrings["SPMSQL"].ConnectionString;

            SqlParameter[] myParams = new SqlParameter[3];
            myParams[0] = new SqlParameter("@scoreRangeID", SqlDbType.Int);
            myParams[0].Value = scoreID;

            myParams[1] = new SqlParameter("@taskID", SqlDbType.VarChar);
            myParams[1].Value = taskID;

            myParams[2] = new SqlParameter("@createdBy", SqlDbType.VarChar);
            myParams[2].Value = createdBy;

            SqlHelper.ExecuteNonQuery(conString, CommandType.StoredProcedure, "SPM_spCreateScoreTaskMapping", myParams);
        }
    }
}
